package com.monkeygroup.mygoplus.view.home.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.commit.thing.vo.PropertyCommitVO;
import com.monkeygroup.mygoplus.common.imageview.ImageLoader;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedCarportActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cumt on 2017/6/7.
 */

public class PublishedImageActivity extends BaseActivity implements View.OnClickListener{

    public static void gotoActivity(Activity activity,String position,String parent ,String type,String cartport) {
        Intent intent = new Intent(activity,
                PublishedImageActivity.class);

        intent.putExtra("type", type);
        intent.putExtra("parent", parent);
        intent.putExtra("position", position);
        intent.putExtra("cartport", cartport);

        activity.startActivityForResult(intent,1);
    }

    private Activity activity;
    private ImageView imgBack;
    private TextView txtTitle;

    private LinearLayout ll_choose_img_item_I,ll_choose_img_item_II,ll_choose_img_item_III;
    private ImageView img_content_I,img_content_II,img_content_III;
    private TextView txt_I,txt_II,txt_III;

    private PropertyCommitVO requestVO;

    private String cartport;
    private String type;
    private String parent;
    private String position;

    private String[] imgList=new String[3];

    private PopupWindow pop;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.published_image);
    }

    @Override
    public void initView() {

        activity = PublishedImageActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);

        ll_choose_img_item_II= (LinearLayout) findViewById(R.id.ll_choose_img_item_II);
        img_content_II= (ImageView) findViewById(R.id.img_content_II);
        txt_II= (TextView) findViewById(R.id.txt_II);

        ll_choose_img_item_I= (LinearLayout) findViewById(R.id.ll_choose_img_item_I);
        img_content_I= (ImageView) findViewById(R.id.img_content_I);
        txt_I= (TextView) findViewById(R.id.txt_I);

        ll_choose_img_item_III= (LinearLayout) findViewById(R.id.ll_choose_img_item_III);
        img_content_III= (ImageView) findViewById(R.id.img_content_III);
        txt_III= (TextView) findViewById(R.id.txt_III);

//        ListView img_list = (ListView) findViewById(R.id.img_list);
//
//        ImageAdapter adapter = new ImageAdapter(activity);
//        img_list.setAdapter(adapter);
        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        type = bundle.getString("type");
        parent= bundle.getString("parent");
        position= bundle.getString("position");
        cartport= bundle.getString("cartport");
        Log.e("cartportcartport====",""+type+parent+position+cartport);
        initData();

        initPopwindow();
    }


    private void initData(){
        //住宅
        if("1".equals(parent)){
            requestVO= LocalInfo.getHouseProperty(activity,type);
        }
        //店面
        else  if("2".equals(parent)){
            requestVO= LocalInfo.getShopProperty(activity,type);
        }
        else  if("3".equals(parent)){
            requestVO= LocalInfo.getOfficeProperty(activity,type);
        }
        else  if("4".equals(parent)){
            requestVO= LocalInfo.getWorkshopProperty(activity,type);
        }
        else  if("6".equals(parent)){
            requestVO= LocalInfo.getLandProperty(activity,type);
        }
        else  if("5".equals(parent)){
            requestVO= LocalInfo.getCarportProperty(activity,type,cartport);
        }
        //社區實景圖
        if("0".equals(position)){
            imgList=requestVO.getPropertyimg_path1();
            txtTitle.setText("社區實景圖");
        }
        else if("1".equals(position)){
            imgList=requestVO.getPropertyimg_path2();
            txtTitle.setText("效果圖");
        }
        else if("2".equals(position)){
            imgList=requestVO.getPropertyimg_path3();
            txtTitle.setText("規劃圖");
        }
        else if("3".equals(position)){
            imgList=requestVO.getPropertyimg_path4();
            txtTitle.setText("位置圖");
        }
        else if("4".equals(position)){
            imgList=requestVO.getPropertyimg_path5();
            txtTitle.setText("生活機能圖");
        }
        else if("5".equals(position)){
            imgList=requestVO.getPropertyimg_path6();
            txtTitle.setText("樣品屋圖");
        }
        else if("6".equals(position)){
            imgList=requestVO.getPropertyimg_path7();
            txtTitle.setText("室內實景圖");
        }
        Log.e("imgListimgList==",""+imgList[0]+imgList.length);

        for(int i=0;i<imgList.length;i++){
            if(!StringUtil.isBlank(imgList[i])){
                if(i==0){
                    ll_choose_img_item_I.removeAllViews();
                    ll_choose_img_item_I.addView(createView(imgList[0]));
                }
                else  if(i==1){
                    ll_choose_img_item_II.removeAllViews();
                    ll_choose_img_item_II.addView(createView(imgList[1]));
                }
                else  if(i==2){
                    ll_choose_img_item_III.removeAllViews();
                    ll_choose_img_item_III.addView(createView(imgList[2]));
                }
            }

        }

    }


    @Override
    public void setListener() {

    }
    private SimpleDraweeView createView(String path){

        SimpleDraweeView imgview = new SimpleDraweeView(activity);
        imgview.setScaleType(ImageView.ScaleType.FIT_XY);

        ImageLoader.loadFile(imgview,path,200,200);

        imgview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (pop != null && pop.isShowing()) {
                    pop.dismiss();
                }
                else{
                    pop.showAsDropDown(txtTitle);
                }

                return false;
            }
        });

        return imgview;
    }

    @Override
    public void doEvent() {
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private class ImageAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        public ImageAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return imgList.length;
        }

        @Override
        public Object getItem(int i) {
            return imgList[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View  convertView = mInflater.inflate(R.layout.img_item, null);

            ImageView img_content = (ImageView)convertView.findViewById(R.id.img_content);
            TextView txt = (TextView)convertView.findViewById(R.id.txt);
            LinearLayout  ll_choose_img_item = (LinearLayout)convertView.findViewById(R.id.ll_choose_img_item);
            LinearLayout  ll_choose= (LinearLayout)convertView.findViewById(R.id.ll_choose);

            LinearLayout.LayoutParams txtlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            SimpleDraweeView imgview = new SimpleDraweeView(activity);
            imgview.setScaleType(ImageView.ScaleType.FIT_XY);
            if(StringUtil.isBlank(imgList[i])){
                txt.setVisibility(View.VISIBLE);
                imgview.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
                imgview.setImageResource(R.mipmap.btn_est_upload_nor2x);
            }
            else{
                imgview.setLayoutParams(txtlp);
                txt.setVisibility(View.GONE);
//                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//                view.setImageURI(Uri.parse("file://"+imgs[position].toString()));
                ImageLoader.loadFile(imgview,imgList[i].toString(),200,200);
            }
            imgview.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (pop != null && pop.isShowing()) {
                        pop.dismiss();
                    }
                    else{
                        pop.showAsDropDown(txtTitle);
                    }

                    return false;
                }
            });
            ll_choose.addView(imgview);


            return convertView;
        }
    }



    private void initPopwindow(){
        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_choose, null);

        pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        pop.setTouchable(true);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        pop.setBackgroundDrawable(new BitmapDrawable());

        LinearLayout llCamera =  (LinearLayout)contentView.findViewById(R.id.ll_camera);
        LinearLayout llAlbum =  (LinearLayout)contentView.findViewById(R.id.ll_album);
        LinearLayout llChooseCancel =  (LinearLayout)contentView.findViewById(R.id.ll_choose_cancel);

        llCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        llAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageActivity.gotoActivity(activity,"1");

            }
        });
        llChooseCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });


    }


}
